/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.unal.ahnmodeler.view;

import co.edu.unal.ahnmodeler.main.App;
import co.edu.unal.ahnmodeler.model.Node;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;

/**
 * Clase que representa el contenedor de un nodo de la red Ad-Hoc. Permite visualizar el nodo y su rango de cobertura.
 * @author raider
 */
public class PNode extends JPanel{

    public static final int RADIUS=10;
    private Node node;
            
    /**
     * Constructor de la clase.
     * @param node Nodo que representa el nodo lógico en la red Ad-Hoc.
     */
    public PNode(Node node) {
        this.node=node;
        initComponents();
        setBounds(node.getStartPoint().x - node.getRange(), node.getStartPoint().y - node.getRange(), node.getRange()*2, node.getRange()*2);
    }
    
    @Override
    public void paint(Graphics g){
        if (node.isSelected()) {
            g.setColor(Color.red);
            g.fillRect(0, 0, 5, 5);
            g.fillRect(getWidth()-5, 0, getWidth(), 5);
            g.fillRect(0, getHeight()-5, 5, getHeight());
            g.fillRect(getWidth()-5, getHeight()-5, getWidth(), getHeight());
        }
        g.setColor(node.getColor());
        g.fillOval((getWidth()-RADIUS)/2, (getHeight()-RADIUS)/2, RADIUS, RADIUS);
        Graphics2D g2 = (Graphics2D) g;
        float dash1[] = {5.0f};
        BasicStroke dashed =
        new BasicStroke(1.0f,
                        BasicStroke.CAP_BUTT,
                        BasicStroke.JOIN_MITER,
                        1.0f, dash1, 0.0f);
        g2.setStroke(dashed);
        g2.drawOval(0, 0, (node.getRange()*2)-1, (node.getRange()*2)-1);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
        node.setSelected(true);
        App.getmFrame().getpModel().repaint();
    }//GEN-LAST:event_formMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
